﻿using System;
using System.Collections.Generic;
using System.Text;
using CadStrategy2013.DxfData;
using MyTool.FileOpt;

namespace CadStrategy2013.CgqStyle
{
    public class YpJsClass : Cgq2013
    {
        public YpJsClass(string secname)
            : base(secname)
        {
        }

        public YpJsClass(string secname, Dictionary<string, string> dic)
            : base(secname, dic)
        {
        }

        private string m_sytle;

        //public override void GetCfg()
        //{
        //    base.GetCfg();
        //    GetCfg("无绝缘接收");
        //}
        public override List<string> ReadTemplateDxf()
        {
            return ReadDxf(Path + "\\template\\无绝缘接收.dll");
        }

        public override string GetSaveFileName(int index)
        {
            if (ShiJiTuZhiCount == 1)
                return "\\无绝缘接收.dxf";
            return "\\无绝缘接收" + (index + 1).ToString() + ".dxf";
            //if (index == 0)
            //    return "\\无绝缘接收.dxf";
            //else
            //    return "\\无绝缘接收" + index + ".dxf";
        }

        private DxfDataClass GetUmlDxfData(DxfDataClass dxfDataClass, IniFile inifile)
        {
            int sum0 = inifile.GetInt("um71", "sum", 0);
            string[] strGRDingXing1 = inifile.GetString("um71", "轨入定型1").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string[] strGRDingXing2 = inifile.GetString("um71", "轨入定型2").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string[] strXRDingXing = inifile.GetString("um71", "限入定型").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            m_sytle = "um71";
            for (int i = 0; i < sum0; i++)
            {
                string strs = inifile.GetString("um71", (i + 1).ToString());
                string[] s = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (s[0].Contains("+") || s[0].Contains("＋"))
                    continue;
                int n = s.Length;
                DevClass devClass = new DevClass();
                devClass.DevName = s[0];
                try
                {
                    devClass.peiList.Add(s[6]);
                    devClass.peiList.Add(s[7]);
                    if (s[2] == "2")
                    {
                        devClass.peiList.Add(s[1] + strGRDingXing2[0]);
                        devClass.peiList.Add(s[1] + strGRDingXing2[1]);
                    }
                    else
                    {
                        devClass.peiList.Add(s[1] + strGRDingXing1[0]);
                        devClass.peiList.Add(s[1] + strGRDingXing1[1]);
                    }
                    devClass.peiList.Add(strXRDingXing[0]);
                    devClass.peiList.Add(strXRDingXing[1]);
                }
                catch
                {
                    devClass.peiList.Clear();
                    for (int i1 = 0; i1 < 6; i1++)
                    {
                        devClass.peiList.Add("");
                    }
                }
                dxfDataClass.devList.Add(devClass);
            }
            if (sum0 != 0)
            {
                ShiJiBanCount = (dxfDataClass.devList.Count - 1) / BanDevCount + 1;
                ShiJiTuZhiCount = (ShiJiBanCount - 1) / BanCount + 1;
            }
            //else
            //{
            //    ShiJiBanCount = 0;
            //    ShiJiTuZhiCount = 0;
            //}
            m_dxfDataClass = dxfDataClass;
            return dxfDataClass;
        }

        private DxfDataClass Get2000DxfData(DxfDataClass dxfDataClass, IniFile inifile)
        {
            int sum0 = inifile.GetInt("2000", "sum", 0);
            string[] strGRDingXing = inifile.GetString("2000", "轨入定型").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string[] strGc1DingXing = inifile.GetString("2000", "轨出1").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string[] strGc2DingXing = inifile.GetString("2000", "轨出2").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            m_sytle = "2000";
            for (int i = 0; i < sum0; i++)
            {
                string strs = inifile.GetString("2000", (i + 1).ToString());
                string[] s = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (s[0].Contains("+") || s[0].Contains("＋"))
                    continue;
                // int n = s.Length;
                DevClass devClass = new DevClass();
                devClass.DevName = s[0];
                try
                {
                    devClass.peiList.Add(s[3] + "-" + s[6]);
                    devClass.peiList.Add(s[3] + "-" + s[7]);
                    devClass.peiList.Add(s[1] + strGRDingXing[0]);
                    devClass.peiList.Add(s[1] + strGRDingXing[1]);
                    devClass.peiList.Add(s[1] + strGc1DingXing[0]);
                    devClass.peiList.Add(s[1] + strGc1DingXing[1]);
                    devClass.peiList.Add("");
                    devClass.peiList.Add("");
                }
                catch
                {
                    devClass.peiList.Clear();
                    for (int i1 = 0; i1 < 6; i1++)
                    {
                        devClass.peiList.Add("");
                    }
                }
                dxfDataClass.devList.Add(devClass);
            }
            if (sum0 != 0)
            {
                ShiJiBanCount = (dxfDataClass.devList.Count - 1) / BanDevCount + 1;
                ShiJiTuZhiCount = (ShiJiBanCount - 1) / BanCount + 1;
            }
            //else
            //{
            //    ShiJiBanCount = 0;
            //    ShiJiTuZhiCount = 0;
            //}
            m_dxfDataClass = dxfDataClass;
            return dxfDataClass;
        }

        public override DxfDataClass GetDxfData()
        {
            DxfDataClass dxfDataClass = new DxfDataClass();
            IniFile inifile = new IniFile("./\\" + StationName + "\\yp.txt");
            inifile.Load();
            dxfDataClass = GetUmlDxfData(dxfDataClass, inifile);
            dxfDataClass = Get2000DxfData(dxfDataClass, inifile);
            return dxfDataClass;
            //int sum0 = inifile.GetInt16("um71", "sum", 0);
            //string[] strGRDingXing1 = inifile.GetString("um71", "轨入定型1", "").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            //string[] strGRDingXing2 = inifile.GetString("um71", "轨入定型2", "").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            //string[] strXRDingXing = inifile.GetString("um71", "限入定型", "").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            //m_sytle = inifile.GetString("um71", "类型", "2000");
            //for (int i = 0; i < sum0; i++)
            //{
            //    DevClass devClass = new DevClass();
            //    string strs = inifile.GetString("um71", (i + 1).ToString(), "");
            //    string[] s = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            //    int n = s.Length;
            //    devClass.DevName = s[0];
            //    try
            //    {
            //        devClass.peiList.Add(s[6]);
            //        devClass.peiList.Add(s[7]);
            //        if (s[2] == "2")
            //        {
            //            devClass.peiList.Add(s[1] + strGRDingXing2[0]);
            //            devClass.peiList.Add(s[1] + strGRDingXing2[1]);
            //        }
            //        else
            //        {
            //            devClass.peiList.Add(s[1] + strGRDingXing1[0]);
            //            devClass.peiList.Add(s[1] + strGRDingXing1[1]);
            //        }
            //        devClass.peiList.Add(strXRDingXing[0]);
            //        devClass.peiList.Add(strXRDingXing[1]);
            //    }
            //    catch (Exception e)
            //    {
            //        devClass.peiList.Clear();
            //        for (int i1 = 0; i1 < 6; i1++)
            //        {
            //            devClass.peiList.Add("");
            //        }
            //    }
            //    dxfDataClass.devList.Add(devClass);
            //}
            //if (sum0 != 0)
            //{
            //    ShiJiBanCount = (dxfDataClass.devList.Count - 1) / BanDevCount + 1;
            //    ShiJiTuZhiCount = (ShiJiBanCount - 1) / BanCount + 1;
            //}
            //else
            //{
            //    ShiJiBanCount = 0;
            //    ShiJiTuZhiCount = 0;
            //}
            //m_dxfDataClass = dxfDataClass;
            //return dxfDataClass;
        }

        public override string ReplaceDev(string str2, int fi = 0)
        {
            string str = str2;
            for (int j = 1; j <= BanCount; j++)
            {
                int nj = j;// -fi * BanCount;
                for (int i = 1; i <= BanDevCount; i++)
                {
                    string str1 = "dev" + nj.ToString() + "-" + i.ToString() + ">";
                    if (str.Contains(str1))
                    {
                        int index = (fi * BanCount + (j - 1)) * BanDevCount + i - 1;
                        if (index >= m_dxfDataClass.devList.Count)
                            return "";
                        try
                        {
                            if (m_sytle == "um71")
                            {
                                if (str.Contains("轨出1"))
                                {
                                    str = str.Replace("轨出1", "限入");
                                }
                                if (str.Contains("轨出2"))
                                {
                                    str = "";
                                }
                            }
                            return str = str.Replace(str1, m_dxfDataClass.devList[index].DevName);
                        }
                        catch
                        {
                            return "";
                        }
                    }
                }
            }
            return str;
        }

        //public override string ReplacePei(string str, int fi = 0)
        //{
        //    for (int j = 1; j <= BanCount; j++)
        //    {
        //        int nj = j;// -fi * BanCount;
        //        for (int i = 1; i <= BanDevPeiCount; i++)
        //        {
        //            string str1 = "pei" + nj.ToString() + "-" + i.ToString() + ">";
        //            if (str.Contains(str1))
        //            {
        //                int index = (fi * BanCount + (j - 1)) * BanDevCount;// +(i - 1) / BanDevPropCount;
        //                if (index >= m_dxfDataClass.devList.Count)
        //                    return "";
        //                int index1 = (i - 1) % BanDevPeiCount;
        //                try
        //                {
        //                    str = str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
        //                }
        //                catch (Exception e)
        //                {
        //                    return "";
        //                }
        //            }
        //        }
        //    }
        //    return str;
        //}
        //public override string ReplacebNo(string str, int fi = 0)
        //{
        //    if (m_sytle == "um71" && str.Contains("GL1-out"))
        //    {
        //        return "";
        //    }
        //    return base.ReplacebNo(str, fi);
        //}
    }
}